<?php
namespace Home\Controller;

use Think\Controller;

/**
 * 景点管理Controller
 * @author DONGGUIYU
 *
 */
class AttractionController extends Controller{
    
    // ======== 景点类型管理 =======>>>>>>>>>
    /**
     * 景点类型列表
     */
     public function attrTypeList(){
         // 查询条件
         // 景点类型名称
         $attrTypeName = I('post.name', "", 'htmlspecialchars');
         if(!empty($attrTypeName)){
         $where['name'] = array('like', "%$attrTypeName%");
             $this->assign("attrTypeName", $attrTypeName);
         }
         // 景点类型
         $attrTypeModel = D('AttractionType');
         // 分页
         $page = setPage($attrTypeModel, $where);
         // 进行分页数据查询
         $orderby['id']='asc';

         // 景点类型
         if(isset($where)){
         $typeList = $attrTypeModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         } else {
         $typeList = $attrTypeModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         }
         $show = $page->show();// 分页显示输出
         $this->assign('page',$show);// 赋值分页输出
         $this->assign("typeList", $typeList);
         $this->display('attractiontype');
    }
    
    /**
    * 添加景点类型
    */
    public function addAttrType(){
         $this->assign('title','添加景点类型');
         $this->display('editattractiontype');
     }
    
     /**
     * 编辑景点
     */
     public function editAttrType(){
        $this->assign('title','修改景点类型');
         // 景点类型ID
         $id = I('get.id', "", 'htmlspecialchars');
        // 景点model
         $attrTypeModel = D("AttractionType");
         $where['id'] = $id;
         $rlt = $attrTypeModel->where($where)->select();
         if(isset($rlt)){
            $this-> assign('attractionType', $rlt[0]);
        }
        $this->display('editattractiontype');
     }
    
    /**
    * 景点类型名称是否已经存在
    */
    public function hasAttrTypeName(){
        // 景点类型名称
        $name = I('post.name', "", 'htmlspecialchars');
        // 景点类型model
        $attrTypeModel = D("AttractionType");
        if(!empty($name)){
            $where['name'] = $name;
            $rlt = $attrTypeModel->where($where)->select();
            if(isset($rlt)){
                // 景点类型名称已存在
                echo "false";
            } else {
                // 景点类型名称不存在
                echo "true";
            }
        }else{
            echo "";
        }
    }
    
    /**
    * 保存景点类型
    */
    public function saveAttrType(){
        // 景点类型ID
        $id =  I('post.attrTypeId', "", 'htmlspecialchars');
        // 景点类型名称
        $name = I('post.name', "", 'htmlspecialchars');

        // 景点model
        $attrTypeModel = D("AttractionType");
        $data['name'] = $name;

        $attrTypeModel->startTrans();
        if(empty($id)){
            // 添加景点
            $rlt = $attrTypeModel->data($data)->add();
        } else {
            // 修改景点
            $where['id'] = $id;
            $rlt = $attrTypeModel->where($where)->save($data);
        }
        if($rlt != 0 && $rlt == false){
            $attrTypeModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            $attrTypeModel->commit();
            $this->success("保存成功！", "attrTypeList");
            exit;
        }
    }

    /**
    * 批量删除景点类型
    */
    public function deleteAttrType(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 景点类型model
        $attrTypeModel = D("AttractionType");
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $attrTypeModel->startTrans();
            foreach ($idArray as $id){
                if($this->checkIsHasAttrByAttrType($id) == false){
                    $where['id'] = $id;
                    $rlt = $attrTypeModel->where($where)->delete();
                    if($rlt == false){
                        // 删除失败时回滚事务
                        $attrTypeModel->rollback();
                        $this->error("删除失败！");
                        exit;
                    }
                } else {
                    $rlt = $attrTypeModel->where($where)->select();
                    $this->error('"'.$rlt[0]['name'].'"类型下含有景点，不能删除！');
                    exit;
                }
            }
            if($rlt != false && $rlt > 0){
                // 提交事务
                $attrTypeModel->commit();
                $this->success("删除成功！", 'attrTypeList');
            }
        }
    }
    
     /**
    * 单个删除景点类型
    */
    public function deleteOneAttrType(){
        $id = I('get.id', "", 'htmlspecialchars');
        // 景点类型model
        $attrTypeModel = D("AttractionType");
        if(intval($id) > 0){
            if($this->checkIsHasAttrByAttrType($id) == false){
                $where['id'] = $id;
                $rlt = $attrTypeModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $attrTypeModel->rollback();
                    $this->error("删除失败！");
                    exit;
                } else {
                    // 提交事务
                    $attrTypeModel->commit();
                    $this->success("删除成功！", 'attrTypeList');
                }
            } else {
                $this->error("该类型下含有景点，不能删除！");
            }
        } else {
            $this->error("没有可删除的景点类型！");
        }
    }
    
    /**
     * 检查指定景点类型下是否含有景点
     * @param int $typeId 景点类型ID
     */
    private function checkIsHasAttrByAttrType($typeId){
        $attractionModel = D('Attraction');
        if(intval($typeId) > 0){
            $where['attraction_type_id'] = $typeId;
            $rlt = $attractionModel->where($where)->select();
            if(isset($rlt) && count($rlt) > 0){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    // ======== 景点类型管理 =======<<<<<<<<<
        
    // ======== 景点管理 =========>>>>>>>>>
    /**
     * 景点列表
     */
    public function attractionList(){
        // 查询条件
        // 景点名称
        $attrName = I('post.name', "", 'htmlspecialchars'); 
        // 景点类型ID
        $attrTypeId = I('post.type', "", 'htmlspecialchars');
        // 城市ID
        $cityId = I('post.city', "", 'htmlspecialchars');
        if(!empty($attrName)){
            $where['name'] = array('like', "%$attrName%");
            $this->assign("name", $attrName);
        }
        if(intval($attrTypeId) > 0){
            $where['attraction_type_id'] = $attrTypeId;
            $this->assign("attraction_type_id", $attrTypeId);
        }
        if(intval($cityId) > 0){
            $where['city_id'] = $cityId;
            $this->assign("city_id", $cityId);
        }
        
        // 景点model
        $attractionModel = new \Home\Model\AttractionModel();
        
        // 分页
        $page = setPage($attractionModel, $where);
        // 进行分页数据查询
        $orderby['update_time']='desc';
        // 景点
        if(isset($where)){
            $rlt = $attractionModel->relation('attrType')->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        } else {
            $rlt = $attractionModel->relation('attrType')->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        }
        $show = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        
        $this->assign("attractionList", $rlt);
        // 景点类型
        $attrTypeModel = D('AttractionType');
        $typeList = $attrTypeModel->select();
        $this->assign("typeList", $typeList);
        // 城市
        $cityModel = D('City');
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('attraction');
    }
    
    /**
     * 添加景点
     */
    public function addAttr(){
        $this->assign('title','添加景点');
        // 景点类型
        $attrTypeModel = D('AttractionType');
        $typeList = $attrTypeModel->select();
        $this->assign("typeList", $typeList);
        // 城市
        $cityModel = D('City');
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('editattraction');
    }
    
    /**
     * 编辑景点
     */
    public function editAttr(){
        $this->assign('title','景点信息');
        // 景点ID
        $id = I('get.id', "", 'htmlspecialchars');
        // 景点model
        $attractionModel = D("Attraction");
        $where['id'] = $id;
        $rlt = $attractionModel->where($where)->order("update_time desc")->select();
        if(isset($rlt)){
            $rlt[0]['cost_explain'] = html_entity_decode($rlt[0]['cost_explain']);
            $rlt[0]['feature'] = html_entity_decode($rlt[0]['feature']);
            $rlt[0]['order_note'] = html_entity_decode($rlt[0]['order_note']);
            $rlt[0]['discount'] = html_entity_decode($rlt[0]['discount']);
            $rlt[0]['releated_product'] = html_entity_decode($rlt[0]['releated_product']);
            $this-> assign('attraction', $rlt[0]);
        }
        // 景点类型
        $attrTypeModel = D('AttractionType');
        $typeList = $attrTypeModel->select();
        $this->assign("typeList", $typeList);
        // 城市
        $cityModel = D('City');
        $cityList = $cityModel->select();
        $this->assign("cityList", $cityList);
        $this->display('editattraction');
    }
    
    /**
     * 景点名称是否已经存在
     */
    public function hasAttrName(){
        $id = I('post.id', "", 'htmlspecialchars');
        // 景点名称
        $name = I('post.name', "", 'htmlspecialchars');
        // 景点类型
        $typeId = I('post.typeId', "", 'htmlspecialchars');
        // 所在城市
        $cityId = I('post.cityId', "", 'htmlspecialchars');
        // 景点model
        $attractionModel = D("Attraction");
        if(!empty($name)){
            $where['name'] = $name;
            if(!empty($typeId)){
                $where['attraction_type_id'] = $typeId;
            }
            if(!empty($cityId)){
                $where['city_id'] = $cityId;
            }
            $rlt = $attractionModel->where($where)->select();
            if(isset($rlt)){
                if($rlt[0]['id'] == $id){
                    echo "true";
                } else {
                    // 景点名称已存在
                    echo "false";
                }
            } else {
                // 景点名称不存在
                echo "true";
            }
        }else{
            echo "";
        }
    }
    
    /**
     * 保存景点
     */
    public function saveAttr(){
        // 景点ID
        $id =  I('post.attractionId', "", 'htmlspecialchars');
        // 景点名称
        $name = I('post.name', "", 'htmlspecialchars');
        // 景点类型
        $typeId = I('post.type', "", 'htmlspecialchars');
        // 所在城市
        $cityId = I('post.city', "", 'htmlspecialchars');
        // 联系电话
        $telePhone = I('post.tele_phone', "", 'htmlspecialchars');
        // 电子邮箱
        $email = I('post.email', "", 'htmlspecialchars');
        // 地址
        $address = I('post.address', "", 'htmlspecialchars');
        // 景点介绍
        $detail = I('post.detail', "", 'htmlspecialchars');
        // 是否推荐
        $is_recommend = I('post.is_recommend', "", 'htmlspecialchars');
        // 价格
        $price = I('post.price', "", 'htmlspecialchars');
        // 景点特色
        $feature = I('post.feature', "", 'htmlspecialchars');
        $feature = removeEmptyP($feature);
        // 预订须知
        $order_note = I('post.order_note', "", 'htmlspecialchars');
        $order_note = removeEmptyP($order_note);
        // 费用说明
        $cost_explain = I('post.cost_explain', "", 'htmlspecialchars');
        $cost_explain = removeEmptyP($cost_explain);
        // 优惠活动
        $discount = I('post.discount', "", 'htmlspecialchars');
        $discount = removeEmptyP($discount);
        // 相关产品
        $releated_product = I('post.releated_product', "", 'htmlspecialchars');
        $releated_product = removeEmptyP($releated_product);

        // 景点model
        $attractionModel = D("Attraction");
        $data['name'] = $name;
        $data['attraction_type_id'] = $typeId;
        $data['city_id'] = $cityId;
        $data['tele_phone'] = $telePhone;
        $data['email'] = $email;
        if(mb_strlen($detail,"utf8") > 300){
            $data['about'] = mb_substr($detail, 0, 300,"utf8");
        } else {
            $data['about'] =$detail;
        }
        $data['address'] = $address;
        $data['detail'] = $detail;
        $data['is_recommend'] = $is_recommend;
        $data['price'] = $price;
        $data['feature'] = $feature;
        $data['order_note'] = $order_note;
        $data['cost_explain'] = $cost_explain;
        $data['discount'] = $discount;
        $data['releated_product'] = $releated_product;
        $attractionModel->startTrans();
        if(empty($id)){
            // 添加景点
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = $data['create_time'];
            $rlt = $attractionModel->data($data)->add();
        } else {
            // 修改景点
            $where['id'] = $id;
            $data['update_time'] = date('Y-m-d H:i:s');
            $rlt = $attractionModel->where($where)->save($data);
        }
        $rootPath = './Public/Uploads/';
        if( $rlt == false){
            $attractionModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            if (empty($id)){
                $dirName =  base64_encode("attraction_".$rlt);
            } else {
                $dirName = $attractionModel->where($where)->getField("img_folder_path");
            }
            if(file_exists($rootPath.$dirName)){
                if(!is_dir($rootPath.$dirName)){
                    mkdir($rootPath.$dirName);
                }
            } else{
                mkdir($rootPath.$dirName);
            }
            // 文件上传
            $config = array(
                'maxSize'    =>    2145728, // 2M
                'rootPath'   =>    $rootPath,
                'savePath'   =>    '',
                'saveName'   =>    '',//array('uniqid', ''),
                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                'autoSub'    =>    true,
                'subName'    =>    $dirName,
                'replace'    =>    true,
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            
            if(!empty($_FILES["file1"]["name"])
              || !empty($_FILES["file2"]["name"])
              || !empty($_FILES["file3"]["name"])
              || !empty($_FILES["file4"]["name"])
              || !empty($_FILES["file5"]["name"])
              || !empty($_FILES["file6"]["name"])
              || !empty($_FILES["file7"]["name"])){
                // 上传文件
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $attractionModel->rollback();
                    rmdir($rootPath.$dirName);
                    $this->error($upload->getError());
                }else{
                    // 上传成功 获取上传文件信息
                    if(!empty($_FILES["file1"]["name"])){
                        $data['first_img_path'] =$info['file1']['savepath'].$info['file1']['savename'];
                    }
                    if(!empty($_FILES["file2"]["name"])){
                        $data['second_img_path'] =$info['file2']['savepath'].$info['file2']['savename'];
                    }
                    if(!empty($_FILES["file3"]["name"])){
                        $data['third_img_path'] =$info['file3']['savepath'].$info['file3']['savename'];
                    }
                    if(!empty($_FILES["file4"]["name"])){
                        $data['forth_img_path'] =$info['file4']['savepath'].$info['file4']['savename'];
                    }
                    if(!empty($_FILES["file5"]["name"])){
                        $data['fifth_img_path'] =$info['file5']['savepath'].$info['file5']['savename'];
                    }
                    if(!empty($_FILES["file6"]["name"])){
                        $data['sixth_img_path'] =$info['file6']['savepath'].$info['file6']['savename'];
                    }
                    if(!empty($_FILES["file7"]["name"])){
                        $data['seventh_img_path'] =$info['file7']['savepath'].$info['file7']['savename'];
                    }
                }
            }
            if(empty($id)){
                // 添加景点
                $where['id'] = $rlt;
            }
            $data['img_folder_path'] = $dirName;
            $rs = $attractionModel->where($where)->save($data);
            if($rs != 0 && $rs == false){
                $attractionModel->rollback();
                $this->error("保存失败！");
                exit;
            } else {
                $attractionModel->commit();
                $this->success("保存成功！", "attractionList");
                exit;
            }
        }
    }
    
    /**
     * 删除景点
     */
    public function deleteAttr(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 景点model
        $attractionModel = D("Attraction");
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $attractionModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $imgFolderPath = "./Public/Uploads/".$attractionModel->where($where)->getField("img_folder_path");
                $rlt = $attractionModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $attractionModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
                // 删除图片文件
                if(deleteDir($imgFolderPath) == false){
                    // 删除失败时回滚事务
                    $attractionModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $attractionModel->commit();
            $this->success("删除成功！", 'attractionList');
        }
    }
    // ======== 景点管理 =========<<<<<<<<<<
    
    // ======== 周边游类型管理 =======>>>>>>>>>
    /**
     * 周边游类型列表
     */
     public function aroundTypeList(){
         // 查询条件
         // 周边游类型名称
         $aroundTypeName = I('post.name', "", 'htmlspecialchars');
         if(!empty($aroundTypeName)){
         $where['name'] = array('like', "%$aroundTypeName%");
             $this->assign("aroundTypeName", $aroundTypeName);
         }
         // 景点类型
             $aroundTypeModel = D('AroundType');
         // 分页
         $page = setPage($aroundTypeModel, $where);
         // 进行分页数据查询
         $orderby['id']='asc';
    
         // 周边游类型
         if(isset($where)){
         $typeList = $aroundTypeModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         } else {
         $typeList = $aroundTypeModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         }
         $show = $page->show();// 分页显示输出
         $this->assign('page',$show);// 赋值分页输出
         $this->assign("typeList", $typeList);
         $this->display('aroundtype');
    }
    
    /**
    * 添加周边游类型
    */
    public function addAroundType(){
        $this->assign('title','添加周边游类型');
         $this->display('editaroundtype');
    }
    
    /**
    * 编辑周边游
    */
    public function editAroundType(){
        $this->assign('title','修改周边游类型');
        // 周边游类型ID
        $id = I('get.id', "", 'htmlspecialchars');
         // 周边游model
         $aroundTypeModel = D("AroundType");
         $where['id'] = $id;
         $rlt = $aroundTypeModel->where($where)->select();
         if(isset($rlt)){
             $this-> assign('aroundType', $rlt[0]);
        }
        $this->display('editaroundtype');
     }
    
     /**
     * 周边游类型名称是否已经存在
     */
     public function hasAroundTypeName(){
         // 周边游类型名称
        $name = I('post.name', "", 'htmlspecialchars');
        // 周边游类型model
        $aroundTypeModel = D("AroundType");
        if(!empty($name)){
            $where['name'] = $name;
            $rlt = $aroundTypeModel->where($where)->select();
            if(isset($rlt)){
            // 周边游类型名称已存在
                echo "false";
            } else {
                // 周边游类型名称不存在
                echo "true";
            }
        }else{
            echo "";
        }
    }
    
    /**
    * 保存周边游类型
    */
    public function saveAroundType(){
        // 周边游类型ID
        $id =  I('post.attrTypeId', "", 'htmlspecialchars');
        // 周边游类型名称
        $name = I('post.name', "", 'htmlspecialchars');

        // 周边游model
        $aroundTypeModel = D("AroundType");
        $data['name'] = $name;
    
        $aroundTypeModel->startTrans();
        if(empty($id)){
            // 添加周边游
            $rlt = $aroundTypeModel->data($data)->add();
         } else {
             // 修改周边游
             $where['id'] = $id;
             $rlt = $aroundTypeModel->where($where)->save($data);
         }
         if($rlt != 0 && $rlt == false){
             $aroundTypeModel->rollback();
             $this->error("保存失败！");
             exit;
         } else {
             $aroundTypeModel->commit();
             $this->success("保存成功！", "aroundTypeList");
            exit;
         }
     }
    
    /**
     * 批量删除周边游类型
     */
     public function deleteAroundType(){
         $idList = I('post.idList', "", 'htmlspecialchars');
         // 周边游类型model
         $aroundTypeModel = D("AroundType");
         if(!empty($idList)){
             $idArray = split(',', $idList);
             // 开启事务
             $aroundTypeModel->startTrans();
             foreach ($idArray as $id){
                 if($this->checkIsHasAroundByType($id) == false){
                     $where['id'] = $id;
                     $rlt = $aroundTypeModel->where($where)->delete();
                     if($rlt == false){
                         // 删除失败时回滚事务
                         $aroundTypeModel->rollback();
                         $this->error("删除失败！");
                             exit;
                     }
                 } else {
                     $rlt = $aroundTypeModel->where($where)->select();
                     $this->error('"'.$rlt[0]['name'].'"类型下含有周边游，不能删除！');
                     exit;
                 }
             }
             if($rlt != false && $rlt > 0){
             // 提交事务
                 $aroundTypeModel->commit();
                 $this->success("删除成功！", 'aroundTypeList');
            }
        }
    }
    
     /**
     * 单个删除周边游类型
     */
     public function deleteOneAroundType(){
         $id = I('get.id', "", 'htmlspecialchars');
         // 周边游类型model
         $aroundTypeModel = D("AroundType");
         if(intval($id) > 0){
             if($this->checkIsHasAroundByType($id) == false){
                 $where['id'] = $id;
                 $rlt = $aroundTypeModel->where($where)->delete();
                 if($rlt == false){
                     // 删除失败时回滚事务
                     $aroundTypeModel->rollback();
                     $this->error("删除失败！");
                     exit;
                 } else {
                     // 提交事务
                     $aroundTypeModel->commit();
                     $this->success("删除成功！", 'aroundTypeList');
                 }
             } else {
                 $this->error("该类型下含有周边游，不能删除！");
             }
         } else {
             $this->error("没有可删除的周边游类型！");
         }
     }

     /**
     * 检查指定周边游类型下是否含有周边游
     * @param int $typeId 周边游类型ID
     */
     private function checkIsHasAroundByType($typeId){
        $aroundTypeModel = D('Around');
        if(intval($typeId) > 0){
            $where['around_type_id'] = $typeId;
            $rlt = $aroundTypeModel->where($where)->select();
            if(isset($rlt) && count($rlt) > 0){
                return true;
             } else {
             return false;
             }
         } else {
            return false;
         }
     }
     // ======== 周边游类型管理 =======<<<<<<<<<
    
     // ======== 周边游管理 =========>>>>>>>>>
     /**
     * 周边游列表
     */
     public function aroundList(){
         // 查询条件
         // 周边游名称
         $aroundName = I('post.name', "", 'htmlspecialchars');
         // 周边游类型ID
         $aroundTypeId = I('post.type', "", 'htmlspecialchars');
         // 城市ID
         $cityId = I('post.city', "", 'htmlspecialchars');
         if(!empty($aroundName)){
             $where['name'] = array('like', "%$aroundName%");
             $this->assign("name", $aroundName);
         }
         if(intval($aroundTypeId) > 0){
             $where['around_type_id'] = $aroundTypeId;
             $this->assign("around_type_id", $aroundTypeId);
         }
         if(intval($cityId) > 0){
         $where['city_id'] = $cityId;
         $this->assign("city_id", $cityId);
         }
        
         // 周边游model
         $aroundModel = new \Home\Model\AroundModel();
        
         // 分页
         $page = setPage($aroundModel, $where);
         // 进行分页数据查询
         $orderby['id']='asc'; 
         // 周边游
         if(isset($where)){
            $rlt = $aroundModel->relation('aroundType')->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         } else {
            $rlt = $aroundModel->relation('aroundType')->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         }
         $show = $page->show();// 分页显示输出
         $this->assign('page',$show);// 赋值分页输出
    
         $this->assign("aroundList", $rlt);
         // 周边游类型
         $aroundTypeModel = D('AroundType');
         $typeList = $aroundTypeModel->select();
         $this->assign("typeList", $typeList);
         // 城市
         $cityModel = D('City');
         $cityList = $cityModel->select();
         $this->assign("cityList", $cityList);
         $this->display('around');
    }
    
    /**
    * 添加周边游
    */
    public function addAround(){
        $this->assign('title','添加周边游');
        // 周边游类型
         $aroundTypeModel = D('AroundType');
         $typeList = $aroundTypeModel->select();
         $this->assign("typeList", $typeList);
         // 城市
         $cityModel = D('City');
         $cityList = $cityModel->select();
         $this->assign("cityList", $cityList);
         $this->display('editaround');
     }
    
     /**
         * 编辑周边游
     */
     public function editAround(){
         $this->assign('title','周边游信息');
         // 周边游ID
         $id = I('get.id', "", 'htmlspecialchars');
         // 周边游model
         $aroundModel = D("Around");
         $where['id'] = $id;
         $rlt = $aroundModel->where($where)->order("update_time desc")->select();
         if(isset($rlt)){
            $rlt[0]['cost_explain'] = html_entity_decode($rlt[0]['cost_explain']);
            $rlt[0]['feature'] = html_entity_decode($rlt[0]['feature']);
            $rlt[0]['order_note'] = html_entity_decode($rlt[0]['order_note']);
            $rlt[0]['discount'] = html_entity_decode($rlt[0]['discount']);
            $rlt[0]['releated_product'] = html_entity_decode($rlt[0]['releated_product']);
            $this-> assign('around', $rlt[0]);
         }
         // 周边游类型
         $aroundTypeModel = D('AroundType');
         $typeList = $aroundTypeModel->select();
         $this->assign("typeList", $typeList);
         // 城市
         $cityModel = D('City');
         $cityList = $cityModel->select();
         $this->assign("cityList", $cityList);
         $this->display('editaround');
     }
    
    /**
     * 周边游名称是否已经存在
     */
    public function hasAroundName(){
         $id = I('post.id', "", 'htmlspecialchars');
         // 周边游名称
         $name = I('post.name', "", 'htmlspecialchars');
         // 周边游类型
         $typeId = I('post.typeId', "", 'htmlspecialchars');
         // 所在城市
         $cityId = I('post.cityId', "", 'htmlspecialchars');
         // 周边游model
         $aroundModel = D("Around");
         if(!empty($name)){
             $where['name'] = $name;
             if(!empty($typeId)){
                 $where['around_type_id'] = $typeId;
             }
             if(!empty($cityId)){
                $where['city_id'] = $cityId;
            }
            $rlt = $aroundModel->where($where)->select();
            if(isset($rlt)){
                if($rlt[0]['id'] == $id){
                    echo "true";
                } else {
                    // 周边游名称已存在
                    echo "false";
                }
            } else {
                // 周边游名称不存在
                echo "true";
            }
         }else{
            echo "";
         }
     }

     /**
     * 保存周边游
     */
     public function saveAround(){
    
        // 周边游ID
        $id =  I('post.aroundId', "", 'htmlspecialchars');
        // 周边游名称
        $name = I('post.name', "", 'htmlspecialchars');
        // 周边游类型
        $typeId = I('post.type', "", 'htmlspecialchars');
        // 所在城市
        $cityId = I('post.city', "", 'htmlspecialchars');
        // 周边游介绍
        $detail = I('post.detail', "", 'htmlspecialchars');
        // 是否推荐
        $is_recommend = I('post.is_recommend', "", 'htmlspecialchars');
        // 价格
        $price = I('post.price', "", 'htmlspecialchars');
        // 周边游特色
        $feature = I('post.feature', "", 'htmlspecialchars');
        $feature = removeEmptyP($feature);
        // 预订须知
        $order_note = I('post.order_note', "", 'htmlspecialchars');
        $order_note = removeEmptyP($order_note);
        // 费用说明
        $cost_explain = I('post.cost_explain', "", 'htmlspecialchars');
        $cost_explain = removeEmptyP($cost_explain);
        // 优惠活动
        $discount = I('post.discount', "", 'htmlspecialchars');
        $discount = removeEmptyP($discount);
        // 相关产品
        $releated_product = I('post.releated_product', "", 'htmlspecialchars');
        $releated_product = removeEmptyP($releated_product);
    
        // 周边游model
        $aroundModel = D("Around");
        $data['name'] = $name;
        $data['around_type_id'] = $typeId;
        $data['orign'] = $cityId;
        if(mb_strlen($detail) > 300){
            $data['about'] = mb_substr($detail, 0, 300,"utf8");
        } else {
            $data['about'] =$detail;
        }
        $data['detail'] = $detail;
        $data['is_recommend'] = $is_recommend;
        $data['price'] = $price;
        $data['feature'] = $feature;
        $data['order_note'] = $order_note;
        $data['cost_explain'] = $cost_explain;
        $data['discount'] = $discount;
        $data['releated_product'] = $releated_product;
        $aroundModel->startTrans();
        if(empty($id)){
            // 添加周边游
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = $data['create_time'];
            $rlt = $aroundModel->data($data)->add();
        } else {
            // 修改周边游
            $where['id'] = $id;
            $data['update_time'] = date('Y-m-d H:i:s');
            $rlt = $aroundModel->where($where)->save($data);
        }
        $rootPath = './Public/Uploads/';
        if( $rlt == false){
            $aroundModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            if (empty($id)){
                $dirName =  base64_encode("around_".$rlt);
            } else {
                $dirName = $aroundModel->where($where)->getField("img_folder_path");
            }
            if(file_exists($rootPath.$dirName)){
                if(!is_dir($rootPath.$dirName)){
                    mkdir($rootPath.$dirName);
                }
            } else{
                mkdir($rootPath.$dirName);
            }
            // 文件上传
            $config = array(
                'maxSize'    =>    3145728,
                'rootPath'   =>    $rootPath,
                'savePath'   =>    '',
                'saveName'   =>    '',//uniqid',
                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                'autoSub'    =>    true,
                'subName'    =>    $dirName,
                'replace'    =>    true,
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            if(!empty($_FILES["file1"]["name"])
              || !empty($_FILES["file2"]["name"])
              || !empty($_FILES["file3"]["name"])
              || !empty($_FILES["file4"]["name"])
              || !empty($_FILES["file5"]["name"])
              || !empty($_FILES["file6"]["name"])
              || !empty($_FILES["file7"]["name"])){
                // 上传文件
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $aroundModel->rollback();
                    rmdir($rootPath.$dirName);
                    $this->error($upload->getError());
                }else{
                    // 上传成功 获取上传文件信息
                    if(!empty($_FILES["file1"]["name"])){
                        $data['first_img_path'] =$info['file1']['savepath'].$info['file1']['savename'];
                    }
                    if(!empty($_FILES["file2"]["name"])){
                        $data['second_img_path'] =$info['file2']['savepath'].$info['file2']['savename'];
                    }
                    if(!empty($_FILES["file3"]["name"])){
                        $data['third_img_path'] =$info['file3']['savepath'].$info['file3']['savename'];
                    }
                    if(!empty($_FILES["file4"]["name"])){
                        $data['forth_img_path'] =$info['file4']['savepath'].$info['file4']['savename'];
                    }
                    if(!empty($_FILES["file5"]["name"])){
                        $data['fifth_img_path'] =$info['file5']['savepath'].$info['file5']['savename'];
                    }
                    if(!empty($_FILES["file6"]["name"])){
                        $data['sixth_img_path'] =$info['file6']['savepath'].$info['file6']['savename'];
                    }
                    if(!empty($_FILES["file7"]["name"])){
                        $data['seventh_img_path'] =$info['file7']['savepath'].$info['file7']['savename'];
                    }
                }
            }
            if(empty($id)){
                // 添加周边游
                $where['id'] = $rlt;
            }
            $data['img_folder_path'] = $dirName;
            
            $rs = $aroundModel->where($where)->save($data);
            if($rs != 0 && $rs == false){
                $aroundModel->rollback();
                $this->error("保存失败！");
                exit;
            } else {
                $aroundModel->commit();
                $this->success("保存成功！", "aroundList");
                exit;
            }
        }
    }
    
    /**
     * 删除周边游
     */
    public function deleteAround(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 周边游model
        $aroundModel = D("Around");
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $aroundModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $imgFolderPath = "./Public/Uploads/".$aroundModel->where($where)->getField("img_folder_path");
                $rlt = $aroundModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $aroundModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
                // 删除图片文件
                if(deleteDir($imgFolderPath) == false){
                    // 删除失败时回滚事务
                    $aroundModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $aroundModel->commit();
            $this->success("删除成功！", 'aroundList');
        }
    }
    // ======== 周边游管理 =========<<<<<<<<<<
    
    // ======== 旅游攻略管理 ========>>>>>>>>>
    /**
     * 旅游攻略列表
     */
     public function travelTipList(){
         // 查询条件
         // 游记标题
         $travelTipTitle = I('post.name', "", 'htmlspecialchars');
         if(!empty($travelTipTitle)){
            $where['title'] = array('like', "%$travelTipTitle%");
             $this->assign("travelTipTitle", $travelTipTitle);
         }
         // 旅游攻略Model
         $travelTipModel = D('TravelTip');
         // 分页
         $page = setPage($travelTipModel,$where);
         // 进行分页数据查询
         $orderby['release_time']='desc';
         $rlt = $travelTipModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
         $show = $page->show();// 分页显示输出
         $this->assign('page',$show);// 赋值分页输出
         
         $travelTipList = array();
         foreach ($rlt as $travelTip){
             $item = $travelTip;
             unset($where);
             $userModel = D("User");
             $where['id'] = $travelTip['user_id'];
             $userInfo = $userModel->where($where)->select();
             if(!empty($userInfo)){
                 if(!empty($userInfo[0]['real_name'])){
                     $name_show = $userInfo[0]['real_name'];
                 } else if(!empty($userInfo[0]['username']) && $userInfo[0]['username'] != $userInfo[0]['phone']){
                     $name_show = $userInfo[0]['username'];
                 } else{
                     $name_show = $userInfo[0]['user_code'];
                 }
                 $item['user_name'] = $name_show;
             } else {
                 $item['user_name'] = "";
             }
             
             $travelTipList[] = $item;
         }

         $this->assign("travelTipList", $travelTipList);
         $this->display('traveltip');
    }
    
    /**
     * 旅游攻略详情
     */
    public function travelTipDetail(){
        $id = I('get.id',"",'htmlspecialchars');
        if(intval($id) > 0){
            $where['id'] = $id;
            $travelTipModel = D('TravelTip');
            $rlt = $travelTipModel->where($where)->select();
            $travel_tip_detail = array();
            if(isset($rlt) && count($rlt) > 0){
                $rlt[0]['detail'] = html_entity_decode($rlt[0]['detail']);
                // 更新浏览次数
                $browse_count = $rlt[0]['browse_count'] + 1;
                $data['browse_count'] = $browse_count;
                $rs = $travelTipModel->where($where)->save($data);
                $travel_tip_detail = $rlt[0];
                $user_id = $rlt[0]['user_id'];
                unset($where);
                $where['id'] = $user_id;
                $userModel = D('User');
                $user = $userModel->where($where)->select();
                if(!empty($user[0]['real_name'])){
                    $travel_tip_detail['user_name'] = $user[0]['real_name'];
                } else if(!empty($user[0]['username'])){
                    $travel_tip_detail['user_name'] = $user[0]['username'];
                } else{
                    $travel_tip_detail['user_name'] = $user[0]['user_code'];
                }
    
            }
            $this->assign("travel_tip", $travel_tip_detail);
    
        }
        $this->display('traveltipdetail');
    }
    
    /**
     * 删除旅游攻略
     */
    public function deleteTravelTip(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 旅游攻略MODEL
        $travelTipModel = D('TravelTip');
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $travelTipModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $rlt = $travelTipModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $travelTipModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $travelTipModel->commit();
            $this->success("删除成功！", 'travelTipList');
        }
    }
    // ======== 旅游攻略管理 ========<<<<<<<<<<
        
    
    /**
     * 检查已推荐个数
     */
    public function checkRecommend(){
        $type = I('post.type', "", 'htmlspecialchars');
        $city = I('post.city', "", 'htmlspecialchars');
        unset($where);
        $where['is_recommend'] = 1;
        // 景点
        if(intval($type) == 1){
            $attractionModel = D('Attraction');
            $where['city_id'] = $city;
            $rlt = $attractionModel->where($where)->select();
            if(isset($rlt) && count($rlt) >= 4){
                echo "false";
                exit;
            } else {
                echo "true";
                exit;
            }
        } else if(intval($type) == 2){
            // 周边游
            $aroundModel = D('Around');
            $where['orign'] = $city;
            $rlt = $aroundModel->where($where)->select();
            if(isset($rlt) && count($rlt) >= 2){
                echo "false";
                exit;
            } else {
                echo "true";
                exit;
            }
        } else {
            echo "";
            exit;
        }
        
    }
}

?>